//
//  MyViewController.swift
//  Weather
//
//  Created by 王友俊 on 16/3/14.
//  Copyright © 2016年 wangyj. All rights reserved.
//

import UIKit


class MyViewController: UIViewController,UITextFieldDelegate,UIImagePickerControllerDelegate, UINavigationControllerDelegate{
    
    var txtUser:UITextField!
    var txtPwd:UITextField!
    
    var showType:LoginShowType = LoginShowType.NONE
    
    @IBOutlet weak var imageView: UIImageView!
    let imagePickerController: UIImagePickerController = UIImagePickerController()
    var isFullScreen: Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        //登录框背景
        let mainSize = UIScreen.mainScreen().bounds.size
        
//        let imgLogin = UIImageView(frame: CGRectMake(80,100,211,109))
//        imgLogin.image = UIImage(named: "login.png")
//        imgLogin.layer.masksToBounds = true
//        self.view.addSubview(imgLogin)
        
        self.imageView.frame = CGRectMake(100, 100, 128, 128)
        
        let vLogin = UIView(frame: CGRectMake(15,200,mainSize.width-30,160))
        vLogin.layer.borderWidth = 0.5
        vLogin.layer.borderColor = UIColor.lightGrayColor().CGColor
        vLogin.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(vLogin)
        
        //用户名输入框
        txtUser = UITextField(frame: CGRectMake(30,30,vLogin.frame.size.width-60,44))
        txtUser.delegate = self
        txtUser.layer.cornerRadius = 5
        txtUser.keyboardType = UIKeyboardType.Default
        txtUser.placeholder = "请输入用户名"
        txtUser.clearButtonMode = UITextFieldViewMode.Always
        txtUser.returnKeyType = UIReturnKeyType.Done
        txtUser.layer.borderColor = UIColor.lightGrayColor().CGColor
        txtUser.layer.borderWidth = 0.5
        txtUser.leftView = UIView(frame: CGRectMake(0,0,44,44))
        txtUser.leftViewMode = UITextFieldViewMode.Always
        
        //用户名输入框左侧图标
        let imgUser = UIImageView(frame: CGRectMake(11,11,22,22))
        imgUser.image = UIImage(named: "user.png")
        txtUser.leftView!.addSubview(imgUser)
        vLogin.addSubview(txtUser)
        
        //密码输入框
        txtPwd = UITextField(frame: CGRectMake(30,90,vLogin.frame.size.width-60,44))
        txtPwd.delegate = self
        txtPwd.placeholder = "请输入密码"
        txtPwd.keyboardType = UIKeyboardType.Default
        txtPwd.returnKeyType = UIReturnKeyType.Done
        txtPwd.clearButtonMode = UITextFieldViewMode.Always
        txtPwd.layer.cornerRadius = 5
        txtPwd.layer.borderColor = UIColor.lightGrayColor().CGColor
        txtPwd.layer.borderWidth = 0.5
        txtPwd.secureTextEntry = true
        txtPwd.leftView = UIView(frame: CGRectMake(0,0,44,44))
        txtPwd.leftViewMode = UITextFieldViewMode.Always
        
        //密码输入框左侧图标
        let imgPwd = UIImageView(frame: CGRectMake(11,11,22,22))
        imgPwd.image = UIImage(named: "password.png")
        txtPwd.leftView!.addSubview(imgPwd)
        vLogin.addSubview(txtPwd)
        

    }
    
    //输入框获取焦点开始编辑
    func textFieldDidBeginEditing(textField:UITextField)
    {
        //如果当前是用户名输入
        if textField.isEqual(txtUser){
            if (showType != LoginShowType.PASS)
            {
                showType = LoginShowType.USER
                return
            }
            showType = LoginShowType.USER
            
        }
        
        else if textField.isEqual(txtPwd){
            if (showType == LoginShowType.PASS)
            {
                showType = LoginShowType.PASS
                return
            }
            showType = LoginShowType.PASS
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
}
    func textFieldShouldReturn(textField:UITextField) ->Bool
    {
        textField.resignFirstResponder()
        return true
    }

//    @IBAction func chooseImage(sender: UIButton) {
//        self.imagePickerController.delegate = self
//        self.imagePickerController.allowsEditing = true
//        if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
//            let alertController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
//            var popover = alertController.popoverPresentationController
//            if (popover != nil){
//                popover?.sourceView = sender
//                popover?.sourceRect = sender.bounds
//                popover?.permittedArrowDirections = UIPopoverArrowDirection.Any
//                }
//            let cameraAction: UIAlertAction = UIAlertAction(title: "拍照换头像", style: .Default) { (action: UIAlertAction!) -> Void in
//                self.imagePickerController.sourceType = UIImagePickerControllerSourceType.Camera
//                self.presentViewController(self.imagePickerController, animated: true, completion: nil)
//            }
//            alertController.addAction(cameraAction)
//            let photoLibraryAction: UIAlertAction = UIAlertAction(title: "从相册选择换头像", style: .Default) { (action: UIAlertAction!) -> Void in
//                self.imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
//                self.imagePickerController.navigationBar.barTintColor = UIColor(red: 171/255, green: 202/255, blue: 41/255, alpha: 1.0)
//                self.imagePickerController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
//                self.imagePickerController.navigationBar.tintColor = UIColor.whiteColor()
//                self.presentViewController(self.imagePickerController, animated: true, completion: nil)
//            }
//            alertController.addAction(photoLibraryAction)
//            let cancelAction: UIAlertAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
//            alertController.addAction(cancelAction)
//            presentViewController(alertController, animated: true, completion: nil)}
//        else{
//            let alertController: UIAlertController = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
//            var popover = alertController.popoverPresentationController
//            
//            if (popover != nil){
//                popover?.sourceView = sender
//                popover?.sourceRect = sender.bounds
//                popover?.permittedArrowDirections = UIPopoverArrowDirection.Any
//                }
//        
//        let photoLibraryAction: UIAlertAction = UIAlertAction(title: "从相册选择换头像", style: .Default) { (action: UIAlertAction!) -> Void in
//            
//            self.imagePickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
//            self.imagePickerController.navigationBar.barTintColor = UIColor(red: 171/255, green: 202/255, blue: 41/255, alpha: 1.0)
//        
//    }


//   func touchesBegan(touches:Set<NSObject>,withEvent event:UIEvent)
//{
//     self.txtUser.resignFirstResponder()
//     self.txtPwd.resignFirstResponder()
//}







//登录框状态枚举
enum LoginShowType {
    case NONE
    case USER
    case PASS
}
